package com.tripspins.app.bo;

import android.app.Application;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import com.tripspins.app.helper.FILE_TYPE;
import com.tripspins.app.helper.TRIP_STATUS;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

public class Trip implements Parcelable {
    /**
     *
     */


    private long id;
    private long idOnServer;
    private String description;
    private ArrayList<Pin> pins;
    private Route route;
    private TRIP_STATUS status;
    private long userId;
    private boolean isSynced;
    private ArrayList<Uri> thumbnails;



    public Trip() {
        pins = new ArrayList<Pin>();
        id = -1;
        status = TRIP_STATUS.RUNNING;
        isSynced = false;


    }
    public boolean isSynced() {
        return isSynced;
    }

    public void setSynced(boolean synced) {
        isSynced = synced;
    }

    public TRIP_STATUS getStatus() {
        return status;
    }

    public void setStatus(String status) {
        if(status.equals("R"))
            this.status = TRIP_STATUS.RUNNING;
        else if(status.equals("P"))
            this.status = TRIP_STATUS.PLANNED;
        else if(status.equals("F"))
            this.status = TRIP_STATUS.FINISHED;
        else this.status = TRIP_STATUS.RUNNING;
    }



    public void setDescription(String description) {
        this.description = description;
    }

    public Route getRoute() {
        return route;
    }

    public void setRoute(Route route) {
        this.route = route;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getIdOnServer() {
        return idOnServer;
    }

    public void setIdOnServer(long idOnServer) {
        this.idOnServer = idOnServer;
    }

    public String getDescription() {
        return description;
    }

    public void setTitle(String description) {
        this.description = description;
    }

    public Pin getPinStart() {
        for(int i = 0; i < pins.size(); i++){
            if(pins.get(i).getLongitude()!=0 &&pins.get(i).getLatitude()!=0)
                return pins.get(i);
        }
        return null;
    }

    public Pin getPinFinish() {
        for(int i = pins.size()-1; i >= 0; i--){
            if(pins.get(i).getLongitude()!=0 &&pins.get(i).getLatitude()!=0)
                return pins.get(i);
        }
        return null;
    }

    public ArrayList<Pin> getPins() {
        return pins;
    }

    public void setPins(ArrayList<Pin> pins) {
        this.pins = pins;
    }

   /* public String toString() {

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        StringBuilder sb = new StringBuilder();
        sb.append(format.format(getPinStart().getDate()));
        sb.append(" - ");
        if (status == TRIP_STATUS.FINISHED)
            sb.append(format.format(getPinFinish().getDate()));
        else
            sb.append("...");
        sb.append("\n");
        sb.append(this.description);
        sb.append("\n");
        sb.append(String.valueOf(pins.size()) + " Pin(s)");
        return sb.toString();
    }*/

    public int getCountAttachments() {
        int countAttachments = 0;
        for(Pin p : pins)
        {
            countAttachments += p.getAttachments().size();
        }
        return countAttachments;
    }

    public String getStringDateFromTo() {
        StringBuilder stringDateFromTo = new StringBuilder();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        stringDateFromTo.append(format.format(this.getPinStart().getDate()) + " - ");
        if (status == TRIP_STATUS.FINISHED)
            stringDateFromTo.append(format.format(getPinFinish().getDate()));
        else
            stringDateFromTo.append("...");
        return stringDateFromTo.toString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(id);
        parcel.writeString(description);
        parcel.writeTypedList(pins);
        parcel.writeParcelable(route,i);
        parcel.writeString(status.getValue().toString());
    }
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Trip createFromParcel(Parcel parcel) {
            return new Trip(parcel);
        }

        public Trip[] newArray(int size) {
            return new Trip[size];
        }
    };

    private Trip (Parcel parcel)
    {
        id = parcel.readLong();
        description = parcel.readString();
        pins = new ArrayList<Pin>();
        parcel.readTypedList(pins, Pin.CREATOR);
        route = parcel.readParcelable(Route.class.getClassLoader());
        setStatus(parcel.readString());
    }

    public ArrayList<Uri> getThumbnails()
    {
        if(thumbnails == null){
            thumbnails = new ArrayList<Uri>();
            for(Pin p : pins)
            {
                for(Attachment a : p.getAttachments())
                {
                    if(a.getFileType() == FILE_TYPE.IMAGE)
                    {
                        try{
                            Uri thumbnail = Uri.parse(a.getPath().replace("ATTACHMENTS", "THUMBNAILS"));
                            File file = new File(a.getPath().replace("ATTACHMENTS","THUMBNAILS"));
                            if (file.exists()){
                                thumbnails.add(thumbnail);
                            }
                        }catch(Exception e){}
                    }
                }
            }
        }
        return thumbnails;
    }

    public Uri getRandomPicture()
    {
        int index;
        if(getThumbnails().size() == 0)
            return null;
        if(getThumbnails().size() == 1)
            index = 0;
        else{
            Random random = new Random();
            index = random.nextInt(getThumbnails().size() - 1);
        }
        return  getThumbnails().get(index);
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public int getCountDays()
    {
        try{
            switch(status)
            {
                case RUNNING:
                    Date now = new Date();
                    return (int)((now.getTime() - getPinStart().getDate().getTime())/ (1000 * 60 * 60 * 24));
                case FINISHED:
                    return (int)((getPinFinish().getDate().getTime() - getPinStart().getDate().getTime())/ (1000 * 60 * 60 * 24));
                case PLANNED:
                    return 0;
            }
        } catch(Exception ex){}
        return 0;
    }
}
